<?php

namespace app\warehouse\logic;

use think\facade\Db;

class GoodsshelvesLogic extends BasicLogic
{

    /**
     * @desc   仓库列表
     * @author zhangheng
     * @date   2024-11-30
     */
    public function getGoodsshelvesList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $name             = getIsset($params,'name');
        $where           = [];
        
        if($name){
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $res = Db::name('warehouse_goodsshelves')
            ->where($where)
            ->order("created_at desc,name asc")
            ->paginate($limit);
        $list = $res->items()?:[];

        $partitions = Db::name('warehouse_partition')
            ->field('id,name')
            ->where($where)
            ->select()->toarray();
        $partitions_map = array_column($partitions,'name','id');

        foreach($list as &$v){
            switch ($v['status']) {
                case '0':
                    $v['status'] = '可分配';
                    break;
                case '1':
                    $v['status'] = '被占用';
                    break;
                case '2':
                    $v['status'] = '可回收';
                    break;
                default:
            }
            $v['partition_name'] = $partitions_map[$v['partition_id']];
            if(!empty($v['sku'])){
                $v['skus'] = explode(',',$v['sku']);
            }else{
                $v['skus'] = '';
            }
        }
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }
    /**
     * desc   货架位列表
     * @author zhangheng
     * @date   2025-02-24
     */
    public function GoodsshelvesList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $name             = getIsset($params,'name');
        $status             = getIsset($params,'status');
        $where           = [];
        
        if($name){
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if($status >= 0){
            $where[] = ['status', '=', $status];
        }
        $res = Db::name('warehouse_goodsshelves')
            ->field('name,weight,status,partition_id')
            ->where($where)
            ->order("created_at desc,name asc")
            ->paginate($limit);
        $list = $res->items()?:[];

        //分区
        $partitions = Db::name('warehouse_partition')
            ->field('id,name')
            ->where($where)
            ->select()->toarray();
        $partitions_map = array_column($partitions,'name','id');

        foreach($list as &$v){
            switch ($v['status']) {
                case '0':
                    $v['status'] = '可分配';
                    break;
                case '1':
                    $v['status'] = '被占用';
                    break;
                case '2':
                    $v['status'] = '可回收';
                    break;
                default:
            }
            $v['partition_name'] = $partitions_map[$v['partition_id']];
        }
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc   仓库列表详情
     * @author zhangheng
     * @date   2024-11-30
     */
    public function find($params=[]){
        $id         = getIsset($params,'id');
        $data       = Db::name('warehouse_goodsshelves')
            ->field("*")
            ->where('id',$id)
            ->find();
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc   仓库列表修改
     * @author zhangheng
     * @date   2024-11-30
     */
    public function edit($params=[]){
        $id         = getIsset($params,'id');
        $name           = getIsset($params,'name');
        $partition_id   = getIsset($params,'partition_id');
        $warehouse_id   = getIsset($params,'warehouse_id');
        $remarks        = getIsset($params,'remarks');
        $weight         = getIsset($params,'weight');
        if(!$name) return   $this->jsonMessage(201,false,'货架不能为空!',null);
        if(!$id) return     $this->jsonMessage(201,false,'ID不能为空!',null);
        $update = [
            'name'          =>$name,
            'partition_id'  =>$partition_id,
            'warehouse_id'  =>$warehouse_id,
            'remarks'       =>$remarks,
            'weight'        =>$weight,
            'updated_at'        =>time(),
        ];
        Db::name('warehouse_goodsshelves')->where(['id'=>$id])->update($update);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }

    /**
     * @desc   仓库列表添加
     * @author zhangheng
     * @date   2024-11-30
     */
    public function add($params=[]){
        $name           = getIsset($params,'name');
        $partition_id   = getIsset($params,'partition_id');
        $warehouse_id   = getIsset($params,'warehouse_id');
        $remarks        = getIsset($params,'remarks');
        $weight         = getIsset($params,'weight');
        if(!$name) return  $this->jsonMessage(201,false,'货架不能为空!',null);
        $insert = [
            'name'          =>$name,
            'partition_id'  =>$partition_id ?? 0,
            'warehouse_id'  =>$warehouse_id ?? 0,
            'remarks'       =>$remarks,
            'weight'        =>$weight,
            'created_at'        =>time(),
        ];
        Db::name('warehouse_goodsshelves')->insert($insert);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }









}